package com.zhongfl.crm.lixiao.batch.service;

import com.heptagram.lixiao.bean.response.revisitlog.LXRevisitLogResponse;
import com.zhongfl.crm.lixiao.bean.resolve.ChannelRevisitLogModelResolver;
import com.zhongfl.crm.model.boss.ChannelRevisitLog;
import com.zhongfl.crm.service.LxUserService;
import com.zhongfl.crm.service.boss.ChannelRevisitLogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.stereotype.Service;
import tech.heptagram.channel.api.model.Channel;
import tech.heptagram.channel.api.service.ChannelService;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;


/**
 * 励销渠道跟进记录同步到boss 服务
 *
 * @author tiantian
 * @date 2018/7/12 12:16
 */
@Service
@Slf4j
public class LXChannelRevisitLog2BossSyncService extends LXRevisitLogService {
    @Resource
    private ChannelService channelService;
    @Resource
    private ChannelRevisitLogService channelRevisitLogService;
    @Resource
    private LxUserService lxUserService;

    @Override
    void syncRevisitLogList2Boss(String loggableType, LXRevisitLogResponse result, List<Map<String, Object>> syncErrorList) {
        for (LXRevisitLogResponse.RevisitLog revisitLog : result.getData().getRevisitLogs()) {
            try {
                log.info("开始同步跟进记录，跟进记录id:{}，渠道id:{}，渠道title:{}", revisitLog.getId(), revisitLog.getLoggableId(), revisitLog.getLoggable().getName());

                Channel channel = getChannel(loggableType, revisitLog);
                if (Objects.isNull(channel)) {
                    log.info("boss系统 渠道不存在，暂不同步跟进记录，跳过！");
                    continue;
                }

                this.syncSingleRecord2Boss(revisitLog, channel, syncErrorList);

            } catch (Exception ex) {
                log.error("跟进记录同步错误: {}", ex.getMessage(), ex);
                super.collectErrorMessage(ex, syncErrorList, revisitLog);
            }
        }
    }

    @Override
    public <T> void syncSingleRecord2Boss(LXRevisitLogResponse.RevisitLog revisitLog, T obj, List<Map<String, Object>> syncErrorList) {
        super.syncSingleRecord2Boss(revisitLog, obj, syncErrorList);
    }

    @Override
    <T> void syncRemark(LXRevisitLogResponse.RevisitLog revisitLog, T obj) {
        ChannelRevisitLogModelResolver channelRevisitLogModelResolver = new ChannelRevisitLogModelResolver((Channel) obj,lxUserService,channelRevisitLogService);

        ChannelRevisitLog channelRevisitLog = channelRevisitLogService.findByLxRevisitLogId(revisitLog.getId());
        if (Objects.isNull(channelRevisitLog)) {
            ChannelRevisitLog saveInfo = channelRevisitLogModelResolver.resolve(revisitLog);

            channelRevisitLogService.save(saveInfo);
            //super.syncFiles(revisitLog,saveInfo);
            log.info("跟进备注保存到BOSS成功。 跟进记录id:{}，渠道id:{}，渠道title:{}", revisitLog.getId(), revisitLog.getLoggableId(), revisitLog.getLoggable().getName());
        } else {
            log.info("跟进备注已存在, 不需要保存。 跟进记录id:{}，渠道id:{}，渠道title:{}", revisitLog.getId(), revisitLog.getLoggableId(), revisitLog.getLoggable().getName());
        }
    }

    @Override
    <T> void syncComments(LXRevisitLogResponse.RevisitLog revisitLog, T obj, List<Map<String, Object>> syncErrorList) {
        log.info("开始同步跟进评论, id:{}", revisitLog.getId());
        if (ArrayUtils.isEmpty(revisitLog.getComments())) {
            log.info("没有查询到最新的跟进记录评论，跳过！");
        }

        ChannelRevisitLogModelResolver channelRevisitLogModelResolver = new ChannelRevisitLogModelResolver((Channel) obj,lxUserService,channelRevisitLogService);

        for (LXRevisitLogResponse.Comment comment : revisitLog.getComments()) {
            try {
                ChannelRevisitLog channelComment = channelRevisitLogService.findByLxRevisitLogId(comment.getId());
                if (Objects.nonNull(channelComment)) {
                    log.info("评论记录id:{},已存在，跳过！");
                    continue;
                }
                log.info("开始同步跟进评论记录，评论记录id:{}，父级备注id:{}，渠道id:{}，渠道title:{}", comment.getId(), revisitLog.getId(), revisitLog.getLoggable().getId(), revisitLog.getLoggable().getName());
                ChannelRevisitLog commentSaveInfo = channelRevisitLogModelResolver.resolveComment(revisitLog, comment);
                channelRevisitLogService.save(commentSaveInfo);
                log.info("同步当前评论完成！");
            } catch (Exception ex) {
                LXChannelRevisitLog2BossSyncService.log.error("跟进评论同步错误: {}", ex.getMessage(), ex);
                Map<String, Object> map = new HashMap<>(2);
                map.put("obj", revisitLog);
                map.put("msg", "评论: " + ex.getMessage());
                syncErrorList.add(map);
            }
        }
    }

    private Channel getChannel(String loggableType, LXRevisitLogResponse.RevisitLog revisitLog) {
        switch (loggableType) {
            case "lead":
                return channelService.findByLxLeadId(revisitLog.getLoggableId());
            case "customer":
                return channelService.findByLxCustomerId(revisitLog.getLoggableId());
            default:
                return null;
        }
    }

}
